package org.example.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
szy

*/
@Component
public class AliPayUtil {
    public String alipay(Map map){
        AlipayClient alipayClient =  new DefaultAlipayClient( "https://openapi.alipaydev.com/gateway.do" , "2021000118643822", "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCh8npH84yZOs3/Pwk7JcbLTUnzreGJhFmFbDsOv1OacJYM9pO1h9JyVQRLu6vOAOf2rx3zy+0/VBR0uU/fpRHuEznsuk9nd5cKzUCrXBDTQZRdQubhZzg9Otisvntp1ApSpGUD71e05RoBjzN0GdegdJ5rf/HNal13ao0KkIh244yDRLVzJ5QW872ulYb+qmben8jcZIXkzeNost0c0YD03/BlX5KzgsXak30mTnqvXcQan1OO1fUbFhhzKnDSy6cFxNlTnGmT+h65WWnMF4zSZYTOGGEYDGzmk1SyKawfGl0Vlgow7qaIc3a8Dqyhx0fFQe6KUpkyUYGRATcaaio1AgMBAAECggEBAIestic7cmoZKuWRcPFdl6tRCAiffxkPiVRPNTcIsUjs07juhxcJQBTLBvtPsgK3HZ68XRKXoR5yGWUkrzcFev0CnH+oIsfjF+ZTyL8HNY1XHq33U6g5KpOZjldCifBCVZeg5IQka01AtsXhq9CzqcNikCotmW1nlABsgtb7+Wfnq9YvHJ/sGGA+L9A+borLqPN1ZPJzaNFA5pd5UZ6GlQAdZfzSFx8UIGdVPMRd/rVgVoHe/r/s6lFg+GgEpH7le1QHWgWMq5P9cCUWPDnuSuxr01YgNcgl/SaZHjFbEGwrip45r1kzZp3T9dRkSiexV+ncZSshcj8H/cggvCKPvW0CgYEA0KnkQ3ceDHA20j+0QkNBaT3QcTfakIlX+5T/0wZkf4qNuvh++xJ4F8C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5yCN29BuIcG1iY3O8/+GQ/lYfkEASJeL74CUbbN7vI0tiR+zBSViGg3J88Lo+06CSvheV8PtRFZCV0Md5q6E2bez5MykJrzxvfUjWaW/CKamCvfFIK4JDIEuPG89ZI3PX/QIDAQAB", "RSA2");  //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request

        //同步回调  返回页面
        alipayRequest.setReturnUrl( "http://4517y7396p.qicp.vip:32906/alipay" );
        //异步回调  返回成功与否的状态
        alipayRequest.setNotifyUrl( " http://wmw99j.natappfree.cc/ali/notifyPay" ); //在公共参数中设置回跳和通知地址
        //out-trade-no  订单信息
        //total-amount  订单价格
//        alipayRequest.setBizContent( "{"  +
//                "    \"out_trade_no\":\"20150320010101002\","  +
//                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
//                "    \"total_amount\":88.88,"  +
//                "    \"subject\":\"Iphone6 16G\","  +
//                "    \"body\":\"Iphone6 16G\","  +
//                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
//                "    \"extend_params\":{"  +
//                "    \"sys_service_provider_id\":\"2088511833207846\""  +
//                "    }" +
//                "  }" ); //填充业务参数
        Map payMap = new HashMap();
        payMap.put("out_trade_no",map.get("orderId"));
        payMap.put("product_code","FAST_INSTANT_TRADE_PAY");
        payMap.put("total_amount",map.get("totalPrice"));
        payMap.put("subject","锋迷商城");
        payMap.put("body","千峰");
        String s = JSONObject.toJSONString(payMap);
        alipayRequest.setBizContent(s);
        String form= "" ;
        try  {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
//        httpResponse.setContentType( "text/html;charset=utf-8");
//        httpResponse.getWriter().write(form); //直接将完整的表单html输出到页面
//        httpResponse.getWriter().flush();
//        httpResponse.getWriter().close();
        //返回一个from表单
        return form;
    }

    public Map<String,String> convertRequestParamsToMap(HttpServletRequest request){
        Map<String,String> retMap =new HashMap<>();
        Set<Map.Entry<String,String[]>> entrySet = request.getParameterMap().entrySet();
        for (Map.Entry<String,String[]> entry :entrySet){
            String name = entry.getKey();
            String[] values = entry.getValue();
            int valLen = values.length;
            if(valLen == 1 ){
                retMap.put(name, values[0]);
            }
            else if(valLen>1){
                StringBuilder sb = new StringBuilder();
                for (String val : values){
                    sb.append(",").append(val);
                }
                retMap.put(name,sb.toString().substring(1));
            }
            else {
                retMap.put(name,"");
            }
        }
        System.out.println(retMap);
        return retMap;

    }
}
